import * as THREE from 'three'
export class Points {
  constructor(
    scene,
    { range, count, setPosition, setAnimation, url, opacity, size }
  ) {
    this.scene = scene
    // 范围
    this.range = range
    // 雪花个数
    this.conut = count
    this.opacity = opacity
    this.size = size
    this.pointList = []
    this.setPosition = setPosition
    this.setAnimation = setAnimation
    this.url = url
    this.init()
  }
  init() {
    // 粒子和粒子系统
    // 材质
    this.material = new THREE.PointsMaterial({
      size: this.size, //粒子大小,
      map: new THREE.TextureLoader().load(this.url), // 粒子贴图
      transparent: true,
      opacity: this.opacity,
      depthTest: false //消除loader黑色背景
    })

    // 几何对象
    this.geometry = new THREE.BufferGeometry()
    // 添加定点信息
    for (let i = 0; i < this.conut; i++) {
      const position = new THREE.Vector3(
        Math.random() * this.range - this.range / 2,
        Math.random() * this.range,
        Math.random() * this.range - this.range / 2
      )
      this.setPosition(position)
      this.pointList.push(position)
    }
    this.geometry.setFromPoints(this.pointList)
    this.points = new THREE.Points(this.geometry, this.material)
    this.scene.add(this.points)
  }
  animation() {
    this.pointList.forEach((position) => {
      this.setAnimation(position)
    })
    this.points.geometry.setFromPoints(this.pointList)
  }
}
